package com.bawei.module_documents.manager

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.library_mvi.ui.BaseMviActivity
import com.bawei.module_documents.R
import com.bawei.module_documents.databinding.ActivityFileBinding
import com.bawei.module_documents.manager.entity.RequestEntity
import com.blankj.utilcode.util.SPUtils
import com.bw.documents.manager.adapter.DocumentsAdapter
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.io.File
@Route(path = "/documents/DocumentsActivity")
class FileActivity : BaseMviActivity<ActivityFileBinding>() {
    private lateinit var viewModel:FileViewModel
    private lateinit var  documentsAdapter:DocumentsAdapter
    private val filePath : String by lazy { getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!.absolutePath }

    override fun handleState() {
        lifecycleScope.launch {
            viewModel.states.collect{
                when(it){
                    is FileState.SelFileSuccessState ->{
                        val list = it.list
                        documentsAdapter.setNewInstance(list)
                    }
                    is FileState.Init->{}
                    is FileState.UploadSuccess ->{
                        showMsg("上传成功")
                    }
                }
            }
        }

    }

    override fun getLayout(): Int {
        return R.layout.activity_file
    }

    override fun initView() {
        super.initView()
        viewDataBinding.documentsPageBar.backImg.setOnClickListener { finish() }
        viewDataBinding.documentsPageBar.titleTv.text = "文件"
        viewDataBinding.documentsUploadImg.setOnClickListener{
            //判断是否为管理员
            if (SPUtils.getInstance().getBoolean("admin")) {
                ARouter.getInstance().build("/documents/LocalActivity").navigation(this, 100)
            }else{
                showMsg("你不是管理员不能上传文件")
            }
        }
        val manager = LinearLayoutManager(this)
        manager.orientation = LinearLayoutManager.VERTICAL
        viewDataBinding.documentsRv.layoutManager = manager
        documentsAdapter = DocumentsAdapter()
        documentsAdapter.setOnItemChildCLickListener{adapter,id,position->
            if(id== R.id.documents_delete_layout){
            //删除数据
            }else{
                var str = documentsAdapter.data[position].filePath.substring(
                    documentsAdapter.data[position].filePath.lastIndexOf("."))
                when (str) {
                    ".pdf" -> {
                        val bund = Bundle()
                        bund.putString("path",documentsAdapter.data[position].filePath)
                        ARouter.getInstance().build("/documents/PDFActivity").with(bund).navigation()
                    }
                    ".mp3" -> {
                        val bund = Bundle()
                        bund.putString("path",documentsAdapter.data[position].filePath)
                        ARouter.getInstance().build("/documents/MusicActivity").with(bund).navigation()
                    }
                    ".mp4" -> {
                        val bund = Bundle()
                        bund.putString("path",documentsAdapter.data[position].filePath)
                        ARouter.getInstance().build("/documents/VideoActivity").with(bund).navigation()
                    }
                    else -> {
                        val bund = Bundle()
                        bund.putString("path",documentsAdapter.data[position].filePath)
                        ARouter.getInstance().build("/documents/PhotoActivity").with(bund).navigation()
                    }
                }
            }
        }
        viewDataBinding.documentsRv.adapter = documentsAdapter

    }

    override fun initData() {
        super.initData()
        val requestEntity = RequestEntity(1, 1)
        lifecycleScope.launch{
            viewModel.intents.send(FileIntent.SelFilesIntent(requestEntity))
        }
    }

    override fun generateViewModel() {
        viewModel = ViewModelProvider(this).get(FileViewModel::class.java)
    }

    //回传值
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == 100){
            val bund = data!!.extras
            if (bund!=null){
                val name = bund.getString("name")
                val path = bund.getString("path")
                lifecycleScope.launch {
                    viewModel.intents.send(FileIntent.UploadFilesIntent( RequestEntity(path!!,name!!)))
                }
            }
        }
    }
}